package com.SpringBoot.user.controller;


import com.SpringBoot.Util.Result;
import com.SpringBoot.user.pojo.User;
import com.SpringBoot.user.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bxk
 * @since 2025-09-13
 */
@Controller
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        try {
            System.out.println(Result.success(userService.login(user)));
            return Result.success(userService.login(user));

        }catch (Exception e) {
            return Result.error(409, "登录失败请重新登录");
        }
    }

    @GetMapping("/userInfo")
    public Result userInfo(@RequestParam("userId") Integer userId) {
        User user = userService.userInfo(userId);
        return Result.success(user);
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success("修改成功");
    }

    @PostMapping("/updatePwdByOldPwd")
    public Result updatePwdByOldPwd(@RequestParam Integer userId,
                                    @RequestParam String oldPwd,
                                    @RequestParam String newPwd) {
        return userService.updatePasswordByOldPwd(userId, oldPwd, newPwd);
    }

    @PostMapping("/updatePasswordByEmail")
    public Result selectPasswordByEmail(@RequestParam String email,
                                        @RequestParam Integer userId,
                                        @RequestParam String password,
                                        @RequestParam String subject) {
        return userService.updatePasswordByEmail(userId,password,email,subject);

    }

    @GetMapping("/allUserPage")
    public Result allUserPage(@RequestParam Integer pageNum,@RequestParam Integer size) {
        return userService.selectAllUserByPage(pageNum,size);
    }

    @GetMapping("/getCount")
    public Result getCount(){return userService.getCount();}

}
